﻿Option Strict On
Public Class MetaString
#Region " Property Field "
    'Private _stringIndexID As Guid?

    Public Property DefaultString() As String
    Public Property DefaultText() As String = Nothing
    Public Property LanguageStringID() As Guid = Nothing
    Public Property StringCd() As String
    Public Property StringID() As Guid
    Public Property StringTypeID() As Integer
    Public Property TextFormatID() As Guid = Nothing

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sqlString"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetMetaString(ByVal sqlString As String) As MetaString

        Dim command As SqlCommand = Base.Connection.Command
        Dim reader As SqlDataReader = Nothing
        Dim dtrow As DataTable = New DataTable
        Dim dr As DataRow = Nothing
        '
        Dim ms As MetaString = Nothing
        '
        Try
            command.CommandText = sqlString
            reader = command.ExecuteReader(CommandBehavior.CloseConnection)
            If reader.HasRows Then
                dtrow.Load(reader)
                dr = dtrow.Rows(0)
                ms = New MetaString
                'Pk always there
                ms.StringID = CType(dr("StringID"), Guid)
                ms.StringCd = dr("StringCd").ToString
                ms.DefaultString = dr("String").ToString
                If Not IsDBNull(dr("Text")) Then
                    ms.DefaultText = dr("Text").ToString
                End If
                ms.StringTypeID = CInt(dr("StringTypeID_FK"))
                ms.TextFormatID = CType(dr("TextFormatID_FK"), Guid)
                If Not IsDBNull(dr("LanguageStringID_FK")) Then
                    ms.LanguageStringID = New Guid(dr("LanguageStringID_FK").ToString)
                End If
            End If
        Catch ex As Exception
            Throw New Exception(String.Format("Invalid SQL command: MetaString.GetMetaStringByCode{0} {1}", vbCrLf, command.CommandText))
        Finally
            If reader IsNot Nothing Then
                reader.Close()
            End If
            dtrow.Dispose()
            command.Dispose()
        End Try
        Return ms
    End Function

    Public Shared Function GetMetaStringByID(ByVal id As Guid, Optional ByVal languageID As Guid = Nothing) As MetaString
        Const selectStatement As String = "SELECT MS.StringID, MS.StringCd, COALESCE(MSL.StringLocal, MS.DefaultString) as String , COALESCE(MSL.TextLocal, MS.DefaultText) as Text, MS.StringTypeID_FK, MS.TextFormatID_FK, MSL.LanguageStringID_FK FROM MetaStrings AS MS LEFT JOIN MetaStringLocals AS MSL ON MS.StringID = MSL.StringID_FK AND MSL.LanguageStringID_FK = '{1}' WHERE (MS.StringID = '{0}')"
        If String.IsNullOrEmpty(languageID.ToString) Then
            languageID = Guid.Empty
        End If
        Return GetMetaString(String.Format(selectStatement, id.ToString, languageID.ToString))
    End Function

    Public Shared Function GetMetaStringByCode(ByVal code As String, Optional ByVal languageID As Guid = Nothing) As MetaString

        Const selectStatement As String = "SELECT MS.StringID, MS.StringCd, COALESCE(MSL.StringLocal, MS.DefaultString) as String , COALESCE(MSL.TextLocal, MS.DefaultText) as Text, MS.StringTypeID_FK, MS.TextFormatID_FK, MSL.LanguageStringID_FK FROM MetaStrings AS MS LEFT JOIN MetaStringLocals AS MSL ON MS.StringID = MSL.StringID_FK AND MSL.LanguageStringID_FK = '{1}' WHERE (MS.StringCd = '{0}')"
        If String.IsNullOrEmpty(languageID.ToString) Then
            languageID = Guid.Empty
        End If
        Return GetMetaString(String.Format(selectStatement, code.ToString, languageID.ToString))

        'Dim command As SqlCommand = Base.Connection.Command
        'Dim reader As SqlDataReader = Nothing
        'Dim dtrow As DataTable = New DataTable
        'Dim dr As DataRow = Nothing
        ''
        'Dim ms As MetaString = Nothing
        'If String.IsNullOrEmpty(languageID.ToString) Then
        '    languageID = Guid.Empty
        'End If
        'Try
        '    command.CommandText = String.Format(selectStatement, code, languageID.ToString)
        '    reader = command.ExecuteReader(CommandBehavior.CloseConnection)
        '    If reader.HasRows Then
        '        dtrow.Load(reader)
        '        dr = dtrow.Rows(0)
        '        ms = New MetaString
        '        'Pk always there
        '        ms.StringID = CType(dr("StringID"), Guid)
        '        ms.StringCd = dr("StringCd").ToString
        '        ms.DefaultString = dr("String").ToString
        '        If Not IsDBNull(dr("Text")) Then
        '            ms.DefaultText = dr("Text").ToString
        '        End If
        '        ms.StringTypeID = CInt(dr("StringTypeID_FK"))
        '        ms.TextFormatID = CType(dr("TextFormatID_FK"), Guid)
        '        If Not IsDBNull(dr("LanguageStringID_FK")) Then
        '            ms.LanguageStringID = New Guid(dr("LanguageStringID_FK").ToString)
        '        End If
        '    End If
        'Catch ex As Exception
        '    Throw New Exception(String.Format("Invalid SQL command: MetaString.GetMetaStringByCode{0} {1}", vbCrLf, command.CommandText))
        'Finally
        '    If reader IsNot Nothing Then
        '        reader.Close()
        '    End If
        '    dtrow.Dispose()
        '    command.Dispose()
        'End Try
        'Return ms
    End Function
#End Region
End Class
